package com.zrrd.io.objectStream;

import com.zrrd.io.pojo.Student;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/**
 * @author WanJl
 * @version 1.0
 * @title ObjectOutputStreamDemo01
 * @description 对象序列化流
 * @create 2023/10/15 9:29
 */
public class ObjectOutputStreamDemo01 {
    /**
     *
     * @param obj   要保存的对象
     * @param fileName  保存对象的位置
     */
    public void saveObject(Object obj,String fileName)throws Exception{
        //创建流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));

        //将对象写入到指定文件中
        oos.writeObject(obj);

        //释放资源
        oos.close();
    }

    public static void main(String[] args) throws Exception {
//        User user = new User();
//        user.setName("张三");
//        user.setUsername("zhangsan");
//        user.setPassword("123456");
        Student s=new Student("张三",25,"男");
        new ObjectOutputStreamDemo01().saveObject(s,"D:/zs");
    }
}
